<template>
  <van-popup v-model="isShow" closeable position="left" @close="closeEvent">
    <div class="article-channel">
      <div class="head">
        <h3>我的频道<small>点击进入频道</small></h3>
        <a
          class="edit"
          href="javascript:;"
          @click="edit = !edit"
          :class="{ active: edit }"
          >{{ edit ? '完成' : '编辑' }}</a
        >
      </div>
      <div class="body">
        <!-- <a href="javascript:;" class="active">推荐</a> -->
        <a
          href="javascript:;"
          :class="{ active: activeIndex === i.id }"
          v-for="(i, index) in list"
          :key="i.id"
          @click="enterChannel(i.id)"
          >{{ i.name }}
          <!-- 绑定删除方法,排除当前项与推荐项 -->
          <geek-icon
            @click.native.stop="del(i, index)"
            v-show="edit && activeIndex !== i.idx && index !== 0"
            name="tag-close"
          ></geek-icon>
        </a>
      </div>
      <div class="head" style="margin-top:12px">
        <h3>频道推荐</h3>
      </div>
      <div class="body">
        <a
          href="javascript:;"
          v-for="i in newChannels"
          :key="i.id"
          @click="add(i)"
        >
          {{ i.name }}
        </a>
      </div>
    </div>
  </van-popup>
</template>

<script>
import { channelsAll, userChannelsPut } from '@/api/home.js'
export default {
  name: 'ArticleChannel',
  // 接收传参
  props: {
    list: {
      type: Array,
      default: () => []
    },
    activeIndex: {
      type: Number,
      default: 0
    }
  },
  data () {
    return {
      isShow: false,
      alldChannels: [],
      edit: false
    }
  },
  created () {
    this.getDate()
  },
  computed: {
    // 使用计算属性筛选出想要得数据
    newChannels () {
      return this.alldChannels.filter(item => {
        return !this.list.find(item2 => {
          return item2.id === item.id
        })
      })
    }
  },
  methods: {
    // 使用关闭事件还原编辑状态
    closeEvent () {
      this.edit = false
    },
    // 调用api获取数据并存储
    async getDate () {
      const res = await channelsAll()
      this.alldChannels = res.data.channels
    },
    // 进入频道,通过emit修改索引值，并关闭弹窗
    enterChannel (index) {
      this.$emit('update:activeIndex', index)
      this.isShow = false
    },
    // 添加频道,1.将当前项数据添加到list,2.调用接口,数据要符合接口格式,3.成功提示
    async add (item) {
      this.list.push(item)
      this.list.map((item, index) => {
        item.seq = index
        return item
      })
      await userChannelsPut(this.list)
      this.$toast.success('添加成功')
    },
    // 删除操纵
    async del (item, index) {
      // 删除列表当前项
      this.list.splice(index, 1)
      this.list.map((item, index) => {
        item.seq = index
        return item
      })
      await userChannelsPut(this.list)
      this.$toast.success('删除成功')
    }
  }
}
</script>

<style lang="less" scoped>
.van-popup {
  width: 100%;
  height: 100%;
  .article-channel {
    margin-top: 44px;
    .head {
      padding: 0 16px;
      display: flex;
      justify-content: space-between;
      justify-items: center;
      padding-bottom: 12px;
      h3 {
        font-size: 16px;
        color: #333;
        margin: 0;
        small {
          font-size: 12px;
          color: #999;
          margin-left: 10px;
        }
      }
      .edit {
        float: right;
        height: 22px;
        width: 52px;
        line-height: 22px;
        text-align: center;
        color: #de644b;
        border-radius: 11px;
        border: 1px solid #de644b;
        font-size: 12px;
        &.active {
          color: #fff;
          background: #de644b;
        }
      }
    }
    .body {
      padding: 0 6px 0 16px;
      a {
        display: inline-block;
        padding: 0 8px;
        font-size: 14px;
        color: #3a3948;
        background: #f7f8fa;
        height: 36px;
        line-height: 36px;
        min-width: 78px;
        margin-right: 10px;
        margin-bottom: 12px;
        border-radius: 18px;
        text-align: center;
        &.active {
          color: @geek-color;
        }
        position: relative;
        .geek-icon {
          position: absolute;
          top: -5px;
          right: -5px;
          line-height: 1;
        }
      }
    }
  }
}
</style>
